# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/topics/item-pipeline.html

import config
import shutil, os, errno

import config, dao

from flash7k7k.items import FlashItem

from scrapy.contrib.pipeline.media import MediaPipeline
from scrapy.core.exceptions import DropItem
from scrapy.http import Request
from scrapy import log


class FlashStorePipeline(object):
    def process_item(self, spider, item):

        print 'flash %s' % (item['id'],)

        dao.insert_row('7k7k', item)

        # raise DropItem("bad item instance")

        return item




class ResDownloadPipeline(MediaPipeline):

    def get_media_requests(self, item, info):

        req = []
        # req.append(Request(item['pic']))
        # req.append(Request(item['pic_small']))

        # if item['flash']:
        #     req.append(Request(item['flash']))

        return req


    def item_completed(self, results, item, info):

        for success, response in results:
            if success:
                if response.url == item['flash']:
                    self.save_file(response.body, item['id'], 'swf')
                elif response.url == item['pic']:
                    self.save_file(response.body, item['id'], 'img')
                elif response.url == item['pic_small']:
                    self.save_file(response.body, item['id'], 'imgs')
            else:
                print response
                log.msg('cannot download file ')

        return item


    def save_file(self, data, id, type):
        id = str(id)
        if len(id) == 1:
            id = '0' + id
    
        path = config.DOWNLOAD_DIR + '7k7k/' + str(id)[-2:] + '/'
    
        try:
            os.makedirs(path)
        except OSError, e:
            if e.errno != errno.EEXIST:
                raise e
    
        dst_file = ''
        if type == 'swf':
            dst_file = path + id + '.swf'
        elif type == 'img':
            dst_file = path + id + '.jpg'
        elif type == 'imgs':
            dst_file = path + id + '_s.jpg'
        else:
            raise Exception('bad download type')

        log.msg('downlaod file %s,%s to %s' % (type, id, dst_file))        
    
        tfile = open(dst_file, 'wb')
        tfile.write(data)
        tfile.close()



